package com.example.mdoncall;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 
 * @author Anthony Maniquiz & Laine Nicolas (Smart Telecom Inc. OJT) 
 * @version 1
 * 
 */

public class EditMedicationsActivity extends Activity{
	
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		setContentView(R.layout.activity_editmedications);
		setOrientation();
		
		Intent intent = getIntent();
		String[] meds = intent.getStringArrayExtra("meds");

		EditText condition = (EditText) findViewById(R.id.conditionField);
		condition.setText(meds[0]);
		
		EditText allergy = (EditText) findViewById(R.id.allergyField);
		allergy.setText(meds[1]);
		
		EditText medication = (EditText) findViewById(R.id.medicationField);
		medication.setText(meds[2]);

	}
	
	public void saveInfo(View view){
		Intent intent = new Intent(this, ViewPatientActivity.class);
		
		Intent intentTwo = getIntent();
		String[] info = intentTwo.getStringArrayExtra("info");
		
		EditText conditions = (EditText) findViewById(R.id.conditionField);
		EditText allergies = (EditText) findViewById(R.id.allergyField);
		EditText medications = (EditText) findViewById(R.id.medicationField);
		
		String lName = info[0];
		String fName = info[1];
		String mName = info[2];
		String gender = info[3];
		String bDay = info[4];
		String contact = info[5];
		
		String street = info[6];
		String city = info[7];
		
		int id = intentTwo.getIntExtra("id", 0);
		
		if (conditions == null || allergies == null || medications == null){
			Toast.makeText(getApplicationContext(), "Please fill up all fields", Toast.LENGTH_SHORT).show();
		} else if(conditions != null && allergies != null && medications != null){
			
			String[] medHistory = { conditions.getText().toString(), allergies.getText().toString(), medications.getText().toString() };
			
			PersonDbHelper db = new PersonDbHelper(this);
			db.updateContact(new Contact(fName,lName,mName,bDay, gender,contact,street, city, medHistory[0],medHistory[1],medHistory[2]), id);
			
			db.close();
			
			intent.putExtra("search", id);
			startActivity(intent);
		}
		

	}
	
	public void goHome(View view){
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
	} 
	
	protected void setOrientation() {
    	int current = getRequestedOrientation();
    	if(current != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
    		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    	}
    }

}
